﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using T4UtilityBelt.DataDesigns;
using T4UtilityBelt.DataDesigns.LightSwitch;
using T4UtilityBelt.Serializers;
using T4UtilityBelt.UxDesigns;
using T4UtilityBelt.UxDesigns.Controls;
using T4UtilityBelt.UxDesigns.Screens2;
using T4UtilityBelt.UxDesigns.Specialized;

namespace T4UtilityBelt.Test
{
    public partial class ucSerialize : UserControl
    {
        public ucSerialize()
        {
            InitializeComponent();
        }

        private void Serialize_Click(object sender, EventArgs e)
        {
            try
            {
                //XSerializer<T4UtilityBelt.DataDesigns.EntityModel> xs = new XSerializer<T4UtilityBelt.DataDesigns.EntityModel>();
                XSerializer xs = new XSerializer();

                tbxSerialized.Text = xs.Serialize(LoadUxModel());

                //tbxSerialized.Text = xs.Serialize(LoadUxModelBaseScreen());

            }
            catch (Exception ex)
            {
                tbxMessage.Text = ex.Message;
            }
        }

        protected T4UtilityBelt.DataDesigns.EntityModel LoadModel()
        {
            LightSwitchModel model = new LightSwitchModel();

            LightSwitchEntity entity = new LightSwitchEntity("Session", "Sessions");
            entity.Properties.Add(new StringProperty("Name") {MaxLength = 255});
            entity.Properties.Add(new StringProperty("Description") {MaxLength = 255});
            model.Entities.Add(entity);


            entity = new LightSwitchEntity("Speaker", "Speakers");
            entity.Properties.Add(new StringProperty("Name") {MaxLength = 255});
            entity.Properties.Add(new StringProperty("Title") {MaxLength = 255});
            entity.Properties.Add(new StringProperty("Company") {MaxLength = 255});
            entity.Properties.Add(new StringProperty("Biography") {MaxLength = 255});


            model.Entities.Add(entity);


            entity = new LightSwitchEntity("SessionSpeaker", "SessionSpeakers");
            entity.Properties.Add(new ParentOne("SpeakerId", "Speaker", true));
            entity.Properties.Add(new ParentOne("SessionId", "Session", true));

            model.Entities.Add(entity);

            model.FixModel();

            return model;
        }

        protected UxDesign LoadUxModel()
        {
            UxDesign model = new UxDesign();

            var list = new EntityListScreen("Session", "SessionListEntity");

            var template = new ItemTemplateControl();
            template.Name = "session_ListItem";

            list.ListBox.Templates.Add(template);
            list.ListBox.ItemsSource = new T4UtilityBelt.UxDesigns.Bindings.Binding("Data");

            var dataPanel = new StackPanelControl();
            template.Controls.Add(dataPanel);

            dataPanel.Tap = new T4UtilityBelt.UxDesigns.Bindings.Binding("ReadCommand");
            dataPanel.DataContext = new T4UtilityBelt.UxDesigns.Bindings.Binding("Data");
            dataPanel.Controls.Add(new StringControl("Name"));

            model.Views.Add(list);

            var create = new T4UtilityBelt.UxDesigns.Screens2.EntityCreateScreen("Session", "SessionCreateEntity");
            create.Content.Controls.Add(new LabelControl() {Text = "Name"});
            create.Content.Controls.Add(new StringControl("Name"));
            model.Views.Add(create);


            var read = new T4UtilityBelt.UxDesigns.Screens2.EntityReadScreen("Session", "SessionReadEntity");
            read.Content.Controls.Add(new LabelControl() {Text = "Name"});
            read.Content.Controls.Add(new StringControl("Name") {ReadOnly = true});

            read.Content.Controls.Add(new LabelControl() {Text = "Name"});
            read.Content.Controls.Add(new StringControl("Description") {ReadOnly = true});
            model.Views.Add(read);


            var update = new T4UtilityBelt.UxDesigns.Screens2.EntityUpdateScreen("Session", "SessionUpdateEntity");
            update.Content.Controls.Add(new LabelControl() {Text = "Name"});
            update.Content.Controls.Add(new StringControl("Name"));
            model.Views.Add(update);

            model.FixModel(LoadModel());

            return model;
        }


        protected UxDesign LoadUxModelBaseScreen()
        {
            UxDesign model = new UxDesign();

            var create = new BaseScreen("Session", "SessionCreateEntity");

            create.Content.Controls.Add(new LabelControl() {Text = "Name"});
            create.Content.Controls.Add(new StringControl("Name"));

            create.ActionBar.Controls.Add(new ActionButton()
                {
                    Click = new T4UtilityBelt.UxDesigns.Bindings.Binding("Save"),
                    Text = "Save",
                    IconUri = "Save"
                });

            model.Views.Add(create);

            model.FixModel(LoadModel());

            return model;
        }


        private void btnDeserialize_Click(object sender, EventArgs e)
        {
            XSerializer xs = new XSerializer();

            try
            {
                //UxModel ux = xs.Deserialize<UxModel>(tbxSerialized.Text);

                T4UtilityBelt.UxDesigns.View ux = xs.Deserialize<T4UtilityBelt.UxDesigns.View>(viewText);

                //T4UtilityBelt.DataDesigns.EntityModel ux =
                //    xs.Deserialize<T4UtilityBelt.DataDesigns.EntityModel>(tbxSerialized.Text);

                string text = xs.Serialize(ux);

                if (tbxSerialized.Text != text)
                {
                    tbxMessage.Text = "Incorrectly deserialized";
                    tbxSerialized.Text = text;
                }
                else
                {
                    tbxMessage.Text = "Serialized correctly";
                }

            }
            catch (Exception ex)
            {
                tbxMessage.Text = ex.Message;
            }
        }

        private string text = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<LightSwitchModel Namespace=""TealightV5"" xmlns:p1=""T4UtilityBelt.DataDesigns"" xmlns=""T4UtilityBelt.DataDesigns.LightSwitch"">
  <Entities xmlns="""">
    <LightSwitchEntity Name=""Session"" PluralName=""Sessions"">
      <p1:StringProperty MaxLength=""255"" Name=""Name"" />
      <p1:StringProperty MaxLength=""255"" Name=""Description"" />
    </LightSwitchEntity>
  </Entities>
</LightSwitchModel>";

    

    private
string viewText = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<p1:EntityListScreen xmlns:p1=""T4UtilityBelt.UxDesigns.Screens2"" xmlns:p2=""T4UtilityBelt.UxDesigns.Controls"" xmlns:p3=""T4UtilityBelt.UxDesigns.Specialized"" xmlns=""T4UtilityBelt.UxDesigns""
       Name=""SessionListEntity"" EntityName=""Session"" Title=""SessionListEntity"">
  <ListBox>
    <p2:ListBoxControl ItemsSource=""Data"">
      <Templates>
        <p2:ItemTemplateControl Name=""session_ListItem"">
          <p2:StackPanelControl Tap=""ReadCommand"" DataContext=""Data"">
            <p2:StringControl Text=""{Name, Mode=TwoWay}"" PropertyName=""Name"" ReadOnly=""true"" />
          </p2:StackPanelControl>
        </p2:ItemTemplateControl>
      </Templates>
    </p2:ListBoxControl>
  </ListBox>
  <Header>
    <p3:Header Text=""SessionListEntity"" />
  </Header>
</p1:EntityListScreen>
";
}

}
